import {
  MiddlewareConsumer,
  Module,
  NestModule,
  RequestMethod,
} from '@nestjs/common';
import { ServeStaticModule } from '@nestjs/serve-static';
import { TypeOrmModule } from '@nestjs/typeorm';
import { join } from 'path';
import { ApplyMiddleware } from './common/middleware/apply.middleware';
import { FunctionMiddleware } from './common/middleware/func.middleware';
import { UsersModule } from './users/users.module';
import { RolesModule } from './roles/roles.module';

@Module({
  imports: [
    TypeOrmModule.forRoot({
      type: 'mysql',
      host: '127.0.0.1',
      port: 3306,
      username: 'root',
      password: '123456',
      database: 'knowledge',
      entities: [__dirname + '/entity/*{.js,.ts}'],
      synchronize: true,
      retryAttempts: 6,
    }),
    ServeStaticModule.forRoot({
      rootPath: join(__dirname, '..', 'client'),
      exclude: ['/api/{*test}'],
      serveStaticOptions: {
        fallthrough: false,
      },
    }),
    UsersModule,
    RolesModule,
  ],
  // controllers: [AppController],
  // providers: [AppService],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer
      .apply(ApplyMiddleware, FunctionMiddleware)
      .forRoutes(
        { path: 'users/queryUsers', method: RequestMethod.GET },
        { path: 'users', method: RequestMethod.GET },
      );
  }
}
// export class AppModule {}
